(function (G) {
  window["components_Input_AnySelect_AnySelect"]({
  "component": true,
  "usingComponents": {
    "drawer": "/components/Drawer/Drawer",
    "liststatus": "/components/List/Status/Status"
  }
},{
template:'#TEMPLATE_components_Input_AnySelect_AnySelect',

    props: {
      name: {
        type: String
      },
      index: {
        type: Number
      },
      disabled: {
        type: Boolean,
        default: false
      },
      placeholder:{
        type: String,
        default: ''
      },
      skey:{
        type:String,
        default:'name'
      },
      sitem:{
        type:String,
        default:''
      },
      vkey:{
        type:String,
        default:'_id'
      },
      canClear:{
        type:Boolean,
        default:false
      },
      searchKey:{
        type:String
      },
      url:{
        type:String
      },
      data:{
        type:Object,
        default:{}
      },
      title:{
        type:String,
        default:'请选择'
      }
    },
    data: function () {
      return {
        disClicks:'',
        show:false,
        contentStyle:'',
        search:'',
        listData: G.list().getData()
      }
    },
    methods: {
      filterValue:function(value){
        var vkey = this.getData('vkey'),disClicks = '|';
        for(var i = 0; i < value.length; i ++) disClicks += value[i][vkey] + '|'
        this.setData({ disClicks:disClicks });
      },
      changeSearch:function(event){
        this.setData({ search: event.srcElement ? event.srcElement.value : event.detail.value});
      },
      open:function(event,args){
        if (this.getData('disabled')) return false;
        args = args || {};
        var vkey = this.getData('vkey'),disClicks = '|';
        if(args.value instanceof Array) for(var i = 0; i < args.value.length; i ++) disClicks += args.value[i][vkey] + '|'
        var contentStyle = this.getData('contentStyle');
        if (G.platform == 'Web') {
          var rect = G.getBoundingClientRect(event.srcElement);
          if (!rect.trueWidth) {
            rect.left = e.clientX;
            rect.top = e.clientY;
          }
          contentStyle = 'left:{left}px;bottom:auto;top:{top}px;right:auto;width:300px;background:#fff;box-shadow: 0 2px 4px rgba(0,0,0,0.2);border-radius:3px;'._eval({
            left: rect.left,
            top: rect.top + 40
          })
        }
        args.contentStyle = contentStyle + (args.contentStyle || '');
        args.disClicks = args.disClicks || disClicks;
        args.show = true;
        args.listData = G.list().getData();
        this.setData(args);
        this.load();
      },
      close:function(){
        this.setData({ show: false });
      },
      buildList:function(){
        var _this = this;
        var config = this.getData('listData');
        config.data = null;
        var data = this.getData('data') || {},searchKey = this.getData('searchKey'),skey = this.getData('skey'),sitem = this.getData('sitem');
        data[searchKey] = this.getData('search') || undefined;
        if(data[searchKey]) data[searchKey] = { $regex: data[searchKey] };
        config.get = {
          url:this.getData('url'),
          data: data
        }
        config.idKey = this.getData('vkey');
        var disClicks = this.getData('disClicks');
        config.disClicks = disClicks;
        config.addDataChange = function(){
          if(this[skey] == undefined) this[skey] = G.string(skey)._eval(this);
          if(sitem && this[sitem] == undefined) this[sitem] = G.string(sitem)._eval(this);
          _this.triggerEvent('adddatachange', this);
        }
        return G.list(config);
      },
      load:function(){
        var _this = this;
        this.buildList().load(function(){
          _this.setData({ listData: this.getData() })
        });
      },
      refresh:function(){
        var _this = this;
        this.buildList().refresh(function () {
          _this.setData({ listData:this.getData() })
        });
      },
      checkn: function (event) {
        var _this = this;
        var list = this.buildList();
        list.checkn(event.currentTarget.dataset.index);
        this.setData({ listData: list.getData() })
      },
      clear:function(){
        if (this.getData('disabled')) return false;
        this.setData({ show: false });
        this.triggerEvent('change', { value: [], id: this.getData('name'), index: this.getData('index') });
      },
      submit:function(){
        if (this.getData('disabled')) return false;
        this.setData({ show: false });
        this.triggerEvent('change', { value: this.buildList().val(), id: this.getData('name'), index: this.getData('index') });
      }
    }
  })
})(Y)
